import { createRouter, createWebHashHistory } from 'vue-router'
import LoginPage from '../views/Login.vue'



const routes = [
  {
    path: '/login',
    name: 'LoginPage',
    component: LoginPage
  },
  {
    path: '/register',
    name: 'RegisterPage',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('../views/Layout.vue'),
    children: [
      {
        path: 'apartments',
        name: 'ApartmentManage',
        component: () => import('../views/ApartmentManage.vue'),
        meta: { title: '公寓管理' }
      },
      {
        path: 'properties',
        name: 'PropertyManage',
        component: () => import('../views/PropertyManage.vue'),
        meta: { title: '房产管理' }
      },
      {
        path: 'owner-info',
        name: 'OwnerInfo',
        component: () => import('../views/OwnerInfo.vue'),
        meta: { title: '业主信息' }
      },
      {
        path: 'fees',
        name: 'FeeManage',
        component: () => import('../views/FeeManage.vue'),
        meta: { title: '费用管理' }
      },
      {
        path: 'services',
        name: 'ServiceManage',
        component: () => import('../views/ServiceManage.vue'),
        meta: { title: '服务管理' }
      },
      {
        path: 'events',
        name: 'EventRegistration',
        component: () => import('@/views/EventRegistration.vue')
      },
      {
        path: 'news',
        name: 'NewsBoard',
        component: () => import('../views/NewsBoard.vue')
      },
      {
        path: 'life',
        name: 'LifeBoard',
        component: () => import('../views/LifeBoard.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const publicPages = ['/login', '/register'];
  const authRequired = !publicPages.includes(to.path);
  const loggedIn = localStorage.getItem('user');

  if (authRequired && !loggedIn) {
    return next('/login');
  }
  next();
})

export default router 